package com.ems.controller;

import com.ems.entity.TMenu;
import com.ems.entity.TRoler;
import com.ems.entity.TUser;
import com.ems.entity.TUserRole;
import com.ems.entity.vo.TMenuVo;
import com.ems.service.TMenuService;
import com.ems.service.TUserService;
import com.ems.util.Response;
import com.ems.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZZH
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/t-user")
public class TUserController {

    @Autowired
    private TUserService tUserService;
    @Autowired
    private TMenuService tMenuService;

    //新增用户数据
    @PostMapping("/add")
    public Response add(@RequestBody TUser tUser){
        if(tUserService.findOne(tUser.getUserName())==null){
            tUserService.add(tUser);
            return new Response(200,"添加成功","",null);
        }else {
            return new Response(400,"用户已存在","",null);
        }
    }

    //删除用户数据
    @GetMapping("/delete/{userName}")
    public Response delete(@PathVariable String userName){
        if(tUserService.findOne(userName)!=null){
            tUserService.delete(userName);
            return new Response(200,"删除成功","",null);
        }else {
            return new Response(400,"用户不存在","",null);
        }
    }

    //查找用户数据(单)
    @GetMapping("/findOne/{userName}")
    public ResultUtil get(@PathVariable String userName){
        if (tUserService.findOne(userName)==null){
            return ResultUtil.fail("用户不存在");
        }else {
            return ResultUtil.success(tUserService.findOne(userName));
        }
    }

    //修改用户数据
    @PostMapping("/update")
    public ResultUtil updateUser(@RequestBody TUser tUser) {
        if (tUserService.update(tUser)) {
            return ResultUtil.success("修改成功");
        }else {
            return ResultUtil.fail("修改失败");
        }
    }


    //查找用户数据(打印所有)
    @GetMapping("/findAll")
    public ResultUtil findAll() {
        if (tUserService.findAll()==null){
            return ResultUtil.fail("查找失败");
        }else {
            return ResultUtil.success(tUserService.findAll());
        }
    }


    //登录
    @PostMapping("/login")
    @Transactional
    public Response loginUser(@RequestBody TUser tUser) {
        String userName = tUser.getUserName();
        String password = tUser.getPassword();
        System.out.println("userName=" + userName + "password" + password);
        if (userName.equals("") || password.equals("")) {
            return new Response(100, "用户名或密码不能为空", "", null);
        } else {
            if (tUserService.login(userName, password) != null) {
                //返回权限菜单列表
                //动态路由
                List<TMenuVo> menuList = tMenuService.treeList(userName);
                Integer dd = tUserService.login(userName,password).getRoleId();
                return new Response(200, "登录成功", "角色id为:"+dd, menuList);
            } else {
                return new Response(400, "用户不存在/密码错误", "", null);
            }
        }
    }

    //注册
    @PostMapping("/register")
    public Response register(@RequestBody TUser tUser) {
        String userName = tUser.getUserName();
        String password = tUser.getPassword();
        if (password.equals("") || userName.equals("")) {
            return new Response(100, "用户名或密码不能为空", "",  null);
        } else {
                if (tUserService.registerTest(tUser.getUserName()) == null) {
                tUserService.register(tUser);
                return new Response(200, "注册成功", "你的初始角色id为"+tUserService.registerTest(userName).getRoleId(), null);
            } else {
                return new Response(400, "注册失败", "用户已存在", null);
            }
        }
    }


}

